﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Windows.Controls.Ribbon;
using System.Windows;
using OODCRM.Common;
using System.Data.Linq;

namespace OODCRM
{
    public partial class MainWindow : RibbonWindow
    {

        private void btnCat_tabThemTiemNangDialog_Click(object sender, RoutedEventArgs e)
        {
            switch (dicDialogMode[CRMDialog.ThemTiemNang])
            {
                case DialogMode.Them:
                    {
                        if (dicDialogData.ContainsKey(CRMDialog.ThemTiemNang)
                            && dicDialogData[CRMDialog.ThemTiemNang] != null)
                        {
                            var item = dicDialogData[CRMDialog.ThemTiemNang] as TIEMNANG;
                            if (item == null) break;

                            // update du lieu ko bind dc

                            var chusohuu = cbxChuSoHuu_tabThemTiemNangDialog.SelectedItem as User;
                            if (chusohuu != null)
                                item.DOITUONG.User = chusohuu;

                            // cap nhat du lieu
                            try
                            {
                                
                                item.TinhTrang = cbxTinhTrang_tabThemTiemNangDialog.SelectedItem.ToString();
                                item.DanhGia = cbxDanhGia_tabThemTiemNangDialog.SelectedItem.ToString();
                                dataContext.DIACHIs.InsertOnSubmit(item.DIACHI);
                                dataContext.DIACHIs.InsertOnSubmit(item.DIACHI1);
                                dataContext.TIEMNANGs.InsertOnSubmit(item);
                                dataContext.SubmitChanges();

                                if (item != null)
                                    listTiemNang.Add(item);

                            }
                            catch (Exception ex)
                            {
                                //MessageBox.Show("Không thể thêm dữ liệu vừa điền! Kiểm tra lại hoặc Nạp lại dữ liệu");
                                MessageBox.Show(ex.Message);
                                dataContext = new CRMDataDataContext(Properties.Settings.Default.User_ConnectionString);

                            }
                        }
                    }
                    break;

                case DialogMode.Sua:
                    {
                        var item = dicDialogData[CRMDialog.ThemTiemNang] as LIENHE;
                        if (item == null) break;

                        var chusohuu = cbxChuSoHuu_tabThemTiemNangDialog.SelectedItem as User;
                        if (chusohuu != null)
                            item.DOITUONG.User = chusohuu;

                        try
                        {

                            dataContext.SubmitChanges();
                        }
                        catch (Exception ex)
                        {
                            //MessageBox.Show("Không thể thêm dữ liệu vừa điền! Kiểm tra lại hoặc Nạp lại dữ liệu");
                            MessageBox.Show(ex.Message);
                            dataContext = new CRMDataDataContext(Properties.Settings.Default.User_ConnectionString);
                        }
                    }
                    break;

                default:
                    break;
            }
            tabTiemNang.IsSelected = true;
        }

        private void btnHuyBo_tabThemTiemNangDialog_Click(object sender, RoutedEventArgs e)
        {
            if (dicDialogMode[CRMDialog.ThemTiemNang] == DialogMode.Sua)
            {
                var item = dicDialogData[CRMDialog.ThemTiemNang] as TIEMNANG;
                if (item != null)
                    dataContext.Refresh(RefreshMode.OverwriteCurrentValues, item);

                int index = listTiemNang.IndexOf(item);
                listTiemNang.RemoveAt(index);
                listTiemNang.Insert(index, item);
            }

            tabTiemNang.IsSelected = true;
        }


        #region Auto

        private void cbxQuocGiaCQ_tabThemTiemNangDialog_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (cbxQuocGiaCQ_tabThemTiemNangDialog.SelectedValue == null) return;
            var x = (from qh in ((VITRIDIALY)(cbxQuocGiaCQ_tabThemTiemNangDialog.SelectedValue)).QUANHEVTDLs
                     select qh.VITRIDIALY1);
            if (x != null)
                cbxTinhThanhCQ_tabThemTiemNangDialog.ItemsSource = x.ToList();
        }

        private void cbxTinhThanhCQ_tabThemTiemNangDialog_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (cbxTinhThanhCQ_tabThemTiemNangDialog.SelectedValue == null) return;
            var x = (from qh in ((VITRIDIALY)(cbxTinhThanhCQ_tabThemTiemNangDialog.SelectedValue)).QUANHEVTDLs
                     select qh.VITRIDIALY1);
            if (x != null)
                cbxQuanHuyenCQ_tabThemTiemNangDialog.ItemsSource = x.ToList();
        }

        private void cbxQuocGiaNR_tabThemTiemNangDialog_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (cbxQuocGiaNR_tabThemTiemNangDialog.SelectedValue == null) return;
            var x = (from qh in ((VITRIDIALY)(cbxQuocGiaNR_tabThemTiemNangDialog.SelectedValue)).QUANHEVTDLs
                     select qh.VITRIDIALY1);
            if (x != null)
                cbxTinhThanhNR_tabThemTiemNangDialog.ItemsSource = x.ToList();
        }

        private void cbxTinhThanhNR_tabThemTiemNangDialog_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (cbxTinhThanhNR_tabThemTiemNangDialog.SelectedValue == null) return;
            var x = (from qh in ((VITRIDIALY)(cbxTinhThanhNR_tabThemTiemNangDialog.SelectedValue)).QUANHEVTDLs
                     select qh.VITRIDIALY1);
            if (x != null)
                cbxQuanHuyenNR_tabThemTiemNangDialog.ItemsSource = x.ToList();
        }

        #endregion Auto


    }
}
